﻿#include <QDialog>
#include "CVideoPlayDouble.h"
#include "CVideoState.h"
#include "CGlobal.h"
#include "CVideoLargeDlg.h"
#include "CGlobal.h"
#include "Public.h"

CVideoPlayDouble::CVideoPlayDouble(CDevCamer* pDev)
	: m_pDev(pDev)
{
	ui.setupUi(this);
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";
	ui.labelVideoState->setParent(ui.label);
	ui.labelVideoState->move(5, 5);
	ui.labelVideoState->raise();
	ui.labelVideoState->show();
	ui.labelVideoState->installEventFilter(this); // 为label安装事件过滤器
	m_bRun = false;
	m_bIsPlaying = false;
	m_bIsPlayingHW = false;
	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(slotRefreshUI()));
	initConnect();
}

CVideoPlayDouble::~CVideoPlayDouble()
{
	endPlayVideo();
	endPlayVideoHW();

	if (m_pTimer != nullptr)
	{
		m_pTimer->stop();

		delete m_pTimer;
		m_pTimer = nullptr;
	}
}

void CVideoPlayDouble::initConnect()
{
	connect(ui.pushButtonVideoPlay, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushPlay);
	connect(ui.pushButtonLarge, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushLarge);
	connect(ui.pushButtonPic, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushSnop);

	connect(ui.pushButtonVideoPlayHW, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushPlayHW);
	connect(ui.pushButtonLargeHW, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushLargeHW);
	connect(ui.pushButtonPicHW, &QPushButton::clicked, this, &CVideoPlayDouble::slotPushSnopHW);
}

bool CVideoPlayDouble::startPlayVideo()
{
	if (m_bRun == false)
		return false;
	if (m_bIsPlaying)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = true;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/StopPlay.png"));
	}
	return bTxRet;
}

bool CVideoPlayDouble::endPlayVideo()
{
	if (m_bIsPlaying == false)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.label;
	infoTx.m_hWid = ui.label->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCStopRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = false;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlaying = false;
		ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/play.png"));
	}
	return bTxRet;
}

bool CVideoPlayDouble::startPlayVideoHW()
{
	if (m_bRun == false)
		return false;
	if (m_bIsPlayingHW)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.labelHW;
	infoTx.m_hWid = ui.labelHW->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enIR;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlayingHW = true;
		ui.pushButtonVideoPlayHW->setIcon(QIcon(":/icon/StopPlay.png"));
	}
	return bTxRet;
}

bool CVideoPlayDouble::endPlayVideoHW()
{
	if (m_bIsPlayingHW == false)
		return true;
	SCtrInfo infoTx;
	infoTx.m_pLabel = ui.labelHW;
	infoTx.m_hWid = ui.labelHW->winId();
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCStopRealPlay;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enIR;
	infoTx.m_bCtrSync = false;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet == true)
	{
		m_bIsPlayingHW = false;
		ui.pushButtonVideoPlayHW->setIcon(QIcon(":/icon/play.png"));
	}
	return bTxRet;
}

void CVideoPlayDouble::videoSateClicked()
{
	QDialog oper;
	oper.setWindowTitle(A2T("摄像头状态"));
	oper.setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	oper.setWindowIcon(QIcon(":/icon/Camer.png"));
	//oper.setFixedSize(400, 300); // 宽度400像素，高度300像素
	QGridLayout* pLayout = new QGridLayout();
	CVideoState* pVideoState = new CVideoState(m_pDev->m_pDevData);
	pVideoState->refreshData(true);
	pLayout->addWidget(pVideoState);
	oper.setLayout(pLayout);
	oper.exec();
}

bool CVideoPlayDouble::eventFilter(QObject *watched, QEvent *event)
{
	if (watched == ui.labelVideoState && event->type() == QEvent::MouseButtonRelease) // 判断对象和事件类型
	{
		QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
		if (mouseEvent->button() == Qt::LeftButton)
		{
			// 判断是否为左键
			videoSateClicked();
			return true; // 表示事件已处理
		}
	}
	// 其他事件交给基类处理
	return QObject::eventFilter(watched, event); // 交给默认处理
}

void CVideoPlayDouble::slotPushPlay()
{
	if (m_bRun == false)
		return;
	if (m_bIsPlaying)//如果正在播放视频
	{
		endPlayVideo();

	}
	else//则播放当前选中的节点
	{
		startPlayVideo();
	}
}

void CVideoPlayDouble::slotPushLarge()
{
	CVideoLargeDlg oper(m_pDev,0);
	oper.exec();
}

void CVideoPlayDouble::slotPushSnop()
{
	if (!m_pDev->m_pDevData->m_bLogin)
		return;
	QString sImgUrl = m_pDev->m_pDevData->m_sRecordPath + "Image/";
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCGetImage;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enCamer;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet)
	{
		messageBox(0, A2T("图片抓拍成功，图片保存路径:%1").arg(infoTx.m_sImgUrl));
		return;
	}
	else
	{
		messageBox(1, A2T("图片抓拍失败:%1").arg(infoTx.m_sReData));
	}
}

void CVideoPlayDouble::slotPushPlayHW()
{
	if (m_bRun == false)
		return;
	if (m_bIsPlayingHW)//如果正在播放视频
	{
		endPlayVideoHW();
	}
	else//则播放当前选中的节点
	{
		startPlayVideoHW();
	}
}

void CVideoPlayDouble::slotPushLargeHW()
{
	CVideoLargeDlg oper(m_pDev,1);
	oper.exec();
}

void CVideoPlayDouble::slotPushSnopHW()
{
	if (!m_pDev->m_pDevData->m_bLogin)
		return;
	QString sImgUrl = m_pDev->m_pDevData->m_sRecordPath + "Image/";
	SCtrInfo infoTx;
	infoTx.m_sGuid = m_pDev->m_sDevGuid;
	infoTx.m_nCtrCmd = enCCapture;
	infoTx.m_nCtrDevType = enSubDev1;
	infoTx.m_nSubCtrDevType = enIR;
	infoTx.m_bCtrSync = true;
	infoTx.m_sImgUrl = sImgUrl;
	bool bTxRet = m_pDev->ctrDev(infoTx);
	if (bTxRet)
	{
		messageBox(0, A2T("图片抓拍成功，图片保存路径:%1").arg(infoTx.m_sImgUrl));
		return;
	}
	else
	{
		messageBox(1, A2T("图片抓拍失败"));
	}
}

void CVideoPlayDouble::refreshData(bool bRefresh)
{
	m_bRun = bRefresh;
	if (bRefresh)
	{
		if (!m_pDev->m_pDevData->m_bLogin)
		{
			return;
		}
		if (m_bIsPlaying == false)
		{
			startPlayVideo();
		}
		if (m_bIsPlayingHW == false)
		{
			startPlayVideoHW();
		}
	}
	else
	{
		endPlayVideo();
		endPlayVideoHW();
	}
	if (bRefresh)
	{
		m_pTimer->start(5000);
	}
	else
	{
		m_pTimer->stop();
	}
}

void CVideoPlayDouble::slotRefreshUI()
{
	if (!isVisible())
	{
		return;
	}
	m_pDev->m_pDevData->m_pMutex->lock();
	if (!m_pDev->m_pDevData->m_bLogin || !m_pDev->m_pDevData->m_bBeltDeviation || !m_pDev->m_pDevData->m_bBeltForeign || !m_pDev->m_pDevData->m_bCoalFlow
		|| !m_pDev->m_pDevData->m_bLargeCoal || !m_pDev->m_pDevData->m_bSafetyHelmet || !m_pDev->m_pDevData->m_bSmokeFire || !m_pDev->m_pDevData->m_bSmokeMakeCall)
	{
		ui.labelVideoState->setStyleSheet(m_sAlarmLamp);
	}
	else
	{
		ui.labelVideoState->setStyleSheet(m_sWorkLamp);
	}
	m_pDev->m_pDevData->m_pMutex->unlock();
}
